package com.demo.interceptor;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class DataInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation ai) {
		Map<String, Object> params = new HashMap<String, Object>();
		HttpServletRequest request = ai.getController().getRequest();
		Enumeration<?> enumeration = ai.getController().getRequest().getParameterNames();
		 while (enumeration.hasMoreElements()) {
	            String key = (String) enumeration.nextElement();                     //取得key
	            int index = key.indexOf("[");
	            if(index > 0){
	                String tmp = key.substring(index+1, key.length()-1);
	                String newKey = key.substring(0, index);
	                Map<String, String> map = (Map<String, String>) params.get(newKey);
	                if(null==map){
	                    map = new HashMap<String, String>();
	                }
	                map.put(tmp, request.getParameter(key));
	                params.put(newKey, map);
	            }else{
	                params.put(key, request.getParameter(key));
	            }
	        }
	        
		 request.setAttribute("p", params);

	}

}
